package com.usooft.metaTalent.admin.system;

import com.usooft.framework.core.query.Page;
import com.usooft.framework.core.utils.MapBuilder;
import com.usooft.framework.web.auth.RequiresPermission;
import com.usooft.framework.web.auth.RequiresPermissionDependOn;
import com.usooft.metaTalent.admin.system.form.DictItemCreateForm;
import com.usooft.metaTalent.admin.system.form.DictItemEditForm;
import com.usooft.metaTalent.admin.system.form.DictItemListForm;
import com.usooft.metaTalent.core.system.entity.Dict;
import com.usooft.metaTalent.core.system.entity.DictItem;
import com.usooft.metaTalent.core.system.service.DictItemService;
import com.usooft.metaTalent.core.system.service.DictService;
import com.usooft.metaTalent.core.system.wrapper.DictItemWrapper;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/system/dict-item")
@RequiredArgsConstructor
public class DictItemController {

    private final DictItemService dictItemService;
    private final DictService dictService;

    @GetMapping("/list")
    @RequiresPermission(value = "/system/dict-item/list", description = "数据字典项/查询")
    public ResponseEntity<?> list(DictItemListForm form) {
        Page<DictItemWrapper> page = dictItemService.page(form.buildQuery());
        return ResponseEntity.ok(page);
    }

    @PostMapping("/create")
    @RequiresPermission(value = "/system/dict-item/create", description = "数据字典项/添加")
    public ResponseEntity<?> create(DictItemCreateForm form) {
        Dict dict = dictService.get(form.getDictId());
        dictItemService.create(dict, form.getValue());
        return ResponseEntity.ok(MapBuilder.builder().build());
    }

    @GetMapping("/create/get")
    @RequiresPermissionDependOn("/system/dict-item/create")
    public ResponseEntity<?> createGet() {
        return ResponseEntity.ok(MapBuilder.builder()
                .build());
    }

    @PostMapping("/edit")
    @RequiresPermission(value = "/system/dict-item/edit", description = "数据字典项/修改")
    public ResponseEntity<?> edit(DictItemEditForm form) {
        DictItem dictItem = dictItemService.get(form.getId());
        dictItemService.edit(dictItem, form.getValue());
        return ResponseEntity.ok(MapBuilder.builder().build());
    }

    @GetMapping("/edit/get")
    @RequiresPermissionDependOn("/system/dict-item/edit")
    public ResponseEntity<?> editGet(Long id) {
        DictItem dictItem = dictItemService.get(id);
        return ResponseEntity.ok(MapBuilder.builder()
                .put("source", dictItem)
                .build());
    }

}
